<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 *
 * Created on	:  Jun 15, 2008
 *
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );
RClassesImporter::cls('controllers.AbstractBackEndController');

/**
 * The controller for Users application
 *
 */
class UsersController extends AbstractBackEndController {
	
	var $_model;
	
	function __construct() {
		parent::__construct();
		SysApplication::importModelCls('UsersModel');
		$this->_model = new UsersModel();
		$this->registerTask ( "new", "edit" );
		$this->registerTask ( "apply", "save" );
	}
	
	function display() {
		$this->setTitle ( "Admin management" );
		$this->addToolbarButton ( "new" );
		$this->addToolbarButton ( "edit" );
		$this->addToolbarButton ( "delete" );
		$msg = SysRequest::getVar ( "msg" );

		RCoreImporter::cls( "html.TableControl" );
		RClassesImporter::cls("dbo.dtos.UserDTO" );
		$table = new TableControl ( 'UserDTO', NULL, $_REQUEST, 'usersCtrl', '', 'user_id DESC' );
		//$table->removeField('password');
		//$table->removeField('user_id');
		//RubikHelper::debugPrePrint($table);
		
		$nextExec = $this->getExecuteBy();
		$nextExec = ucfirst($nextExec);
		$table->setLinkRule('username','javascript:edit'.$nextExec.'Item($key)');
		

		$msg = "<p class=\"form-message\" style=\"text-align:left;\">
					{$msg}
			  </p>";
		$tblHtml = $table->createHtmlTable(20);
		eval('echo "'.SysApplication::loadTemplate('back/listUsers').'";');	
	}
	
	function edit($dto = null) {
		$oModel = $this->_model;
		$cid = SysRequest::getVar ( "cid" );
		if (empty( $dto )) { 
			if(is_array($cid)) {
				$cid = $cid[0];
			}				
			$dto = $oModel->getUserDetails($cid);
		}
		
		//$formData = $this->_getEditFormAndPrepareEditTask($dto, 'User');		
		
		
		RCoreImporter::cls( "html.FormFieldsControl" );			
		$formData = new FormFieldsControl( $dto );	
		
		$formData->setFieldHtmlInfo('password',new PasswordHtmlFieldInfo("password"));
		$formData = $this->_getEditFormAndPrepareEditTask($dto, 'User');
		$formData->setFieldHtmlInfo('join_time',$this->_getDateHtmlFieldInfo("join_time"));
		$htmlContent .= $formData->generateHtml();
		
		$msg = SysRequest::getVar('msg');
		eval('echo "'.SysApplication::loadTemplate('back/editUser').'";');	
	}
	
	/**
	 * Save User
	 *
	 */
	function save($isUserEdit = false) {		
		$model = $this->_model;		
		$dto = new UserDTO();
		if (SysRequest::getInt('user_id')) {
			$dto->resetToNull();
		}
		$dto->bind($_POST);

		if ($dto->checkKeyNotDefault()) {
			$dto->setEmptyNumberToZero();
		}
		
//		RubikHelper::debugPrePrint($dto, true);
		$result = $model->saveUserDTO ( $dto );										
		if ($dto->hasError())
			$msg = $dto->getAllErrorsMsg();			

		if ($result === false || $this->getExecuteVal () == 'apply') {
			$_REQUEST ["msg"] = $msg ? $msg : SysLanguage::_ ( "ITEM_SAVE_SUCCESSFUL" );
			return $this->edit ($dto);
		} else {						
			$this->setRedirectToFunction('display', $msg ? $msg : SysLanguage::_ ( "ITEM_SAVE_SUCCESSFUL" ));			
		}				
	}
	function apply($isUserEdit = false) {		
		$model = $this->_model;		
		$dto = new UserDTO();
		if (SysRequest::getInt('user_id')) {
			$dto->resetToNull();
		}
		$dto->bind($_POST);

		if ($dto->checkKeyNotDefault()) {
			$dto->setEmptyNumberToZero();
		}
		
//		RubikHelper::debugPrePrint($dto, true);
		$result = $model->saveUserDTO ( $dto );										
		if ($dto->hasError())
			$msg = $dto->getAllErrorsMsg();			

		if ($result === false || $this->getExecuteVal () == 'apply') {
			$_REQUEST ["msg"] = $msg ? $msg : SysLanguage::_ ( "ITEM_SAVE_SUCCESSFUL" );
			return $this->edit ($dto);
		}			
	}
	
	function delete() {
		$model = $this->_model;
        $arrayIds = SysRequest::getVar ( "cid" );
        $model->delete($arrayIds);
        $this->setRedirectToFunction('display', SysLanguage::_ ( "ITEM_DELETE_SUCCESSFUL" ));
	}	

	
	function toggleActivate() {
		$id = SysRequest::getInt('user_id');
		$model = &$this->_model;
		$model->toggleUser('activate', $id);
		$this->setRedirectToFunction('display');
	}
	

	function _setCommonFields(&$fieldsCtrl) {
//		$fieldsCtrl->setFieldHtmlInfo("date_join",$this->_getDateHtmlFieldInfo('date_join'));
//		$fieldsCtrl->setFieldHtmlInfo('user_group_id', $this->_getReadOnlyFieldInfo('user_group_id'));
	}
	
	function myAccount1() {	//binh them 1
		$this->setTitle('My Account Information');
		
		RClassesImporter::cls('dbo.dtos.UserDTO');
		$user = new UserDTO();
		$user->resetToNull();
		$user->setKeyValue(SysAuthenticate::getUserId());
		
		if (SysRequest::getMethod() == 'POST') {
			$user->bind($_POST);
			$user->setEmptyToNull();
			SysDatabase::saveDTO($user);
		}
		SysDatabase::fillData($user);
		$tpl = SysApplication::loadTemplate('back/myAccount');
		eval('echo "'.$tpl.'";');
	}

}
